<script setup lang="ts">
interface IProps {
  label: string
  checked?: boolean
}

interface IEmits {
  (e: 'update:checked', payload: { label: string, checked: boolean }): void
}

const props = defineProps<IProps>()

const emits = defineEmits<IEmits>()

const label = computed(() => props.label || '')
const checked = computed(() => props.checked || false)
const modelValue = ref(checked.value)

watch(modelValue, (newValue) => {
  modelValue.value = newValue
  emits('update:checked', { label: label.value, checked: newValue })
}, {
  deep: true,
  immediate: true,
})
</script>

<template>
  <label class="checkbox">
    <span mr-2>{{ label }}</span>
    <input v-model="modelValue" v-bind="$attrs" :name="label" type="checkbox">
    <span class="checkmark" />

  </label>
</template>

<style scoped>
.checkbox {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  user-select: none;
  font-size: 16px;
  color: #333;
  transition: color 0.3s;
}

.checkbox input[type='checkbox'] {
  display: none;
}

.checkbox .checkmark {
  width: 24px;
  height: 24px;
  border: 2px solid #333;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
  transition:
    background-color 0.3s,
    border-color 0.3s,
    transform 0.3s;
  transform-style: preserve-3d;
}

.checkbox .checkmark::before {
  content: '\2713';
  font-size: 16px;
  color: transparent;
  transition:
    color 0.3s,
    transform 0.3s;
}

.checkbox input[type='checkbox']:checked + .checkmark {
  background-color: #333;
  border-color: #333;
  transform: scale(1.1) rotateZ(360deg) rotateY(360deg);
}

.checkbox input[type='checkbox']:checked + .checkmark::before {
  color: #fff;
}

.checkbox:hover {
  color: #666;
}

.checkbox:hover .checkmark {
  border-color: #666;
  background-color: #f0f0f0;
  transform: scale(1.05);
}

.checkbox input[type='checkbox']:focus + .checkmark {
  box-shadow: 0 0 3px 2px rgba(0, 0, 0, 0.2);
  outline: none;
}

.checkbox .checkmark,
.checkbox input[type='checkbox']:checked + .checkmark {
  transition:
    background-color 1.3s,
    border-color 1.3s,
    color 1.3s,
    transform 0.3s;
}
</style>
